package zust.se.delicious.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import zust.se.delicious.service.BookmarkService;
import zust.se.delicious.vo.BookmarkBean;
import zust.se.delicious.vo.SessionUserBean;

public class EditLinkAction extends ActionSupport {
	private int bid;
	private BookmarkBean bookmark;
	private boolean success;

	public boolean getSuccess() {
		return true;
	}

	public BookmarkBean getBookmark() {
		return bookmark;
	}

	private BookmarkService bookmarkService;

	public void setBookmark(BookmarkBean bookmark) {
		this.bookmark = bookmark;
	}

	public void setBookmarkService(BookmarkService bookmarkService) {
		this.bookmarkService = bookmarkService;
	}

	public String initLinkForm() {
		bookmark = bookmarkService.getBmById(bid);
		return SUCCESS;
	}

	public String saveLink() {
		Map session = ActionContext.getContext().getSession();
		SessionUserBean sessionUser = (SessionUserBean) session.get("sessionUser");
		int uid;
		if (sessionUser != null) {
			uid = sessionUser.getId();
			if (uid == bookmark.getUser_id()) {
				bookmark.setId(bid);
				bookmarkService.updateBookmark(bookmark);
			} else {
				bookmark.setUser_id(uid);
				bookmarkService.saveBookmark(bookmark);
			}
		}
		return SUCCESS;
	}

	public int getBid() {
		return bid;
	}

	public String getFormUrl() {
		return "saveEditLink?bid=" + bid;
	}

	public void setBid(int bid) {
		this.bid = bid;
	}
}
